﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Lidgren.Network;
using Lidgren.Network.Xna;

namespace Networking.Packets
{
    public class LocationPacket
    {
        private const int BitsPerElementQuaternion = 8;
        public const UInt64 TAG = 0;
        public LocationPacket() { }
        public LocationPacket(UInt64 playerId, Vector3 loc, Quaternion orientationQuaternion) : this(playerId, loc, orientationQuaternion, 0) { }
        public LocationPacket(UInt64 playerId, Vector3 loc) : this(playerId, loc, new Quaternion(0,0,0,0), 0) { }
        public LocationPacket(UInt64 playerId, Vector3 loc, Quaternion orientation, UInt64 senderUniqueId)
        {
            PlayerID = playerId;
            Location = loc;
            SenderId = senderUniqueId;
            Orientation = orientation;
        }
        public UInt64 SenderId { get; set; }
        public UInt64 PlayerID { get; private set; }
        public Vector3 Location { get; private set; }
        public Quaternion Orientation { get; private set; }

        public static void BuildOutputMessage(ref NetOutgoingMessage msg, LocationPacket pack)
        {
            msg.Write(TAG);
            msg.Write(pack.PlayerID);
            XNAExtensions.Write(msg, pack.Location);
            XNAExtensions.WriteRotation(msg, pack.Orientation, BitsPerElementQuaternion);
        }
        /// <summary>
        /// Parses the message and populates the packet with its information. This assumes the TAG has already been stripped from the packet
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="pack"></param>
        public static void ParseMessage(NetIncomingMessage msg, ref LocationPacket pack)
        {
            pack.PlayerID = (UInt64)msg.ReadInt64();
            pack.Location = XNAExtensions.ReadVector3(msg);
            pack.Orientation = XNAExtensions.ReadRotation(msg, BitsPerElementQuaternion);
        }

    }
}
